﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using LitJson;
using System.IO;

namespace weiqijiance.联网通信协议
{
    public class LWbass
    {
        public 数据库操作.dbbass objdb;
        public LWbass()
        {
            objdb = new 数据库操作.dbbass();

        }
        public string sjcxlb = "";
        public mainform m_mainform;// { get; set; }

        public bool bydhh = false;
        public virtual void setmainform(mainform m_m)
        {
            m_mainform = m_m;
        }
        public virtual void init()
        {
        }

        public  string sobdsfkq = "";
        public void readjcyq()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\检测要求设置.ini";
            string path1 = string.Format(@"{0}", path);
            iniclass objini = new iniclass();
            objini.Ini(path1);
            sobdsfkq = "是";// objini.ReadValue("设置", "OBD是否开启");


        }
        public virtual void setclxx(string slsh)//获取维柯车辆信息
        {
            string sql;
            sql = string.Format(@"Select *  from Detects_SJE,Auto where Auto.AT_ID=Detects_SJE.AT_ID and (DETECT_ID={0} or ASM_JYLSH='{0}')  ", slsh);
            DataTable dt = objdb.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int ijycs = 0;
                    int.TryParse(dt.Rows[i]["ITERAT"].ToString().Trim(), out ijycs);
                    string testtimes = string.Format("{0}", ijycs + 1);

                    baocunCLXX(dt.Rows[i]["PLATE_ID"].ToString().Trim(), dt.Rows[i]["PCLASS_ID"].ToString().Trim(), dt.Rows[i]["AT_gl"].ToString().Trim(), dt.Rows[i]["AT_FDJZS"].ToString().Trim(), dt.Rows[i]["AT_ZBZL"].ToString().Trim(), dt.Rows[i]["AT_CLLX"].ToString().Trim());//
                }
            }

        }
        public void baocunCLXX(string hphm, string hpzl, string edgl, string edzs, string jzzl,string cllx)
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\车辆信息.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);

            m_mainform.Writue("设置", "车牌号码", hphm);
            m_mainform.Writue("设置", "号牌种类", hpzl);
            m_mainform.Writue("设置", "额定功率", edgl);
            m_mainform.Writue("设置", "额定转速", edzs);
            m_mainform.Writue("设置", "基准质量", jzzl);
            m_mainform.Writue("设置", "车辆类型", cllx);
            

        }
        public virtual string start(string sjylsh, string stime, string sycy, string sczy, bool bqz)
        {
            string sql;
            sql = string.Format(@"Select *  from Detects_SJE,Auto where Auto.AT_ID=Detects_SJE.AT_ID and (DETECT_ID={0} or ASM_JYLSH='{0}')  ", sjylsh);
            DataTable dt = objdb.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int ijycs = 0;
                    int.TryParse(dt.Rows[i]["ITERAT"].ToString().Trim(), out ijycs);
                    m_mainform.testtimes = string.Format("{0}", ijycs + 1);

                    m_mainform.baocunCLXX(dt.Rows[i]["PLATE_ID"].ToString().Trim(), dt.Rows[i]["PCLASS_ID"].ToString().Trim(), dt.Rows[i]["AT_gl"].ToString().Trim(), dt.Rows[i]["AT_FDJZS"].ToString().Trim(), dt.Rows[i]["AT_ZBZL"].ToString().Trim(), dt.Rows[i]["AT_CLLX"].ToString().Trim(),"");//
                    // getstandxzwk(dt.Rows[i]["AUTO_ID"].ToString().Trim());
                    //objbass.getstandxz(dt.Rows[i]["AUTO_ID"].ToString().Trim());

                }
            }
            return "成功";
        }
        public virtual void zhuapai(string sjylsh, string hphm, string hpzl, string stongdao, string gongwei, string xuhao)
        {
        }//本地抓拍

        public virtual void luxiangkaishi(string sjylsh, string hphm, string hpzl, string stongdao, string gongwei)
        {

        }//本地录像开始

        public virtual void luxiangjieshu(string sjylsh, string stongdao, string gongwei)
        {

        }//本地录像结束

        public virtual bool uploadimage(string sjylsh)
        {
            return true;
        }

        public virtual bool stop(string sjylsh,string jyjg)
        {
            return true;
        }

        public virtual bool cancel(string sjylsh)
        {
            return true;
        }
        public virtual string senddate(string sID, string svalue)
        {
            return "";
        }
       
        public virtual void sendOBDGCSJ(string sjylsh)
        {

        }
       
        public virtual void gettime()//同步时间
        {
        }
        public virtual void XZ001()//检测限值信息
        {
        }
        public virtual string GetState()// 获取检测状态信息接口
        {
            return "";
        }
        public virtual string GetGas()// 尾气待检列表
        {
            return "";
        }
        public virtual string GetObd()// OBD待检列表
        {
            return "";
        }
        public virtual string GetPic(string zpbh)//抓拍
        {
            return "";
        }
        public virtual string LuXiang(string lxbh, string lxlj)//录像
        {
            return "";
        }
        public virtual string SendMessage(string code, string message)// 尾气待检列表
        {
            return "";
        }
        public virtual string GetWJ001()// 外检待检列表
        {
            return "";
        }
        public virtual string GetCarInfo()// 获取待检车辆信息接口
        {
            return "";
        }
        public virtual string WJ001()
        {
            return "";
        }
        public virtual string BgAirHC()//上传HC
        {
            return "";
        }

        public virtual string OBD001()//OBD开始检测
        {
            return "";
        }

        public virtual string WQ001()//尾气开始检测
        {
            return "";
        }

        public virtual string JC011()//尾气开始检测
        {
            return "";
        }
        //public virtual string sqm(string jcxbh)//授权码
        //{
        //    return "";
        //}
        //public virtual bool bbh(string bbh,string sqm)//版本号
        //{
        //    return true;
        //}

        public virtual string JCZT(string szt)//检测状态
        {
            return "";
        }

        public virtual string OBDJC()//OBD 检测人员列表
        {
            return "";
        }
        public virtual string OBDYQ(string sqm, string jczbh)//OBD 检测仪列表
        {
            return "";
        }
        public virtual string OBDDJ(string sqm, string jczbh, string cphm)//OBD 待检列表
        {
            return "";
        }
      
        public virtual void insertjcwc(string slsh)//检测完成标志
        {

        }

        public virtual string sendOBDJCJG(string sjylsh)//上传OBD检测结果
        {
            return "1";
        }

        public virtual void getstandxz(string slsh)//获取限值
        {
            string sql;
            sql = string.Format(@"Select *  from Detects_SJE,Auto where Auto.AT_ID=Detects_SJE.AT_ID and (DETECT_ID={0} or ASM_JYLSH='{0}') order by DETECT_ID desc ", slsh);
            DataTable dt = objdb.GetTable(sql);
            if (dt.Rows.Count > 0)
            {

                    float.TryParse(dt.Rows[0]["STD_HC25"].ToString().Trim(), out m_mainform.HCEL5025);
                    float.TryParse(dt.Rows[0]["STD_CO25"].ToString().Trim(), out m_mainform.COEL5025);
                    float.TryParse(dt.Rows[0]["STD_NO25"].ToString().Trim(), out m_mainform.NOEL5025);
                    float.TryParse(dt.Rows[0]["STD_HC40"].ToString().Trim(), out m_mainform.HCEL2540);
                    float.TryParse(dt.Rows[0]["STD_CO40"].ToString().Trim(), out m_mainform.COEL2540);
                    float.TryParse(dt.Rows[0]["STD_NO40"].ToString().Trim(), out m_mainform.NOEL2540);

                    float.TryParse(dt.Rows[0]["STD_CO"].ToString().Trim(), out m_mainform.LICOL);
                    float.TryParse(dt.Rows[0]["STD_HC"].ToString().Trim(), out m_mainform.LIHCL);
                    float.TryParse(dt.Rows[0]["STD_HCO"].ToString().Trim(), out m_mainform.HICOL);
                    float.TryParse(dt.Rows[0]["STD_HHC"].ToString().Trim(), out m_mainform.HIHCL);
                    //lamdasx = 1.03f;
                    //lamdaxx = 0.97f;
                    float.TryParse(dt.Rows[0]["STD_LUG"].ToString().Trim(), out m_mainform.TypeApprovalLimits);
                    float.TryParse(dt.Rows[0]["STD_BTG"].ToString().Trim(), out m_mainform.InspectionLimits);

                    //float.TryParse(dt.Rows[i]["STD_NOX"].ToString().Trim(), out m_mainform.noxlimit);
                    float.TryParse(dt.Rows[0]["STD_LUGNO"].ToString().Trim(), out m_mainform.noxlimit);

                

            }
        }

        public virtual void getstandxz(string szPlate, string szPClass, string sycy, string sczy)//获取限值
        {
        }

        public virtual string getuser()
        {
            return "";
        }
        public virtual DataTable getjilu(string shphm, string sdatestart, string sdateend, bool bbz)//获取登录车辆上线
        {
            string sql;
            sql = string.Format(@"  Select ASM_JYLSH,
                  DETECT_ID,
                 PLATE_ID,
                 PCLASS_ID,
                 LOGINDATE,
                 ITERAT,
                 JCXM,LOGINDATE,WQPD
                 From Detects_SJE Where ");
            {
                //sql = sql + string.Format(@"  LOGINDATE>'{0}' and LOGINDATE<'{1}' and PLATE_ID like '%%{2}%%' ", DateTime.Now.ToString("yyyy-MM-dd 00:00:00"), DateTime.Now.ToString("yyyy-MM-dd 23:00:00"), shphm);

                sql = sql + string.Format(@"  LOGINDATE>'{0}' and LOGINDATE<'{1}' and PLATE_ID like '%%{2}%%' ", sdatestart, sdateend, shphm);
            }
            sql = sql + " order by DETECT_ID desc ";
            DataTable dt = objdb.GetTable(sql);
            DataTable dtref = new DataTable();
            dtref.Columns.Add("DETECT_ID");
            dtref.Columns.Add("PLATE_ID");
            dtref.Columns.Add("PCLASS_ID");
            dtref.Columns.Add("jcff");
            dtref.Columns.Add("OBD");
            dtref.Columns.Add("jccs");
            dtref.Columns.Add("zt");

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dtref.NewRow();
                    //dr[0] = dt.Rows[i]["DETECT_ID"].ToString().Trim();    //填充第一列的数据 
                    dr[0] = dt.Rows[i]["ASM_JYLSH"].ToString().Trim();    //填充第一列的数据 
                    dr[1] = dt.Rows[i]["PLATE_ID"].ToString().Trim();    //填充第二列的数据 
                    dr[2] = dt.Rows[i]["PCLASS_ID"].ToString().Trim();    //填充第二列的数据 
                    string sjcfa = "ASM工况";
                    if (dt.Rows[i]["JCXM"].ToString().Trim().Contains("ASM"))
                    {
                        sjcfa = "ASM工况";
                    }
                    else if (dt.Rows[i]["JCXM"].ToString().Trim().Contains("SDS"))
                    {
                        sjcfa = "双怠速";
                    }
                    else if (dt.Rows[i]["JCXM"].ToString().Trim().Contains("LUG"))
                    {
                        sjcfa = "加载减速";
                    }
                    else if (dt.Rows[i]["JCXM"].ToString().Trim().Contains("ZYJS"))
                    {
                        sjcfa = "自由加速";
                    }
                    else
                        continue;
                    dr[3] = sjcfa;    // 
                    dr[4] = "";
                    dr[5] = "1";
                    dr[6] = "";
                    dtref.Rows.Add(dr);
                }
            }
            return dtref;
        }
        #region  //旧联网方式  目前可用
        public virtual string uploadASM(联网通信协议.安车model.GC002 gc002, 联网通信协议.安车model.GC011 gc011, 联网通信协议.安车model.JC002 jc002)
        {
            return "";
        }

        public virtual string uploadSDS(联网通信协议.安车model.GC003 gc003, 联网通信协议.安车model.GC011 gc011, 联网通信协议.安车model.JC003 jc003)
        {
            return "";
        }

        public virtual string uploadLUG(联网通信协议.安车model.GC004 gc004, 联网通信协议.安车model.GC011 gc011, 联网通信协议.安车model.JC004 jc004)
        {
            return "";
        }

        public virtual string uploadZYJS(联网通信协议.安车model.GC005 gc005, 联网通信协议.安车model.GC011 gc011, 联网通信协议.安车model.JC005 jc005)
        {
            return "";
        }
        #endregion  //旧联网方式  目前可用

        public virtual string sendOBDGC(联网通信协议.安车model.GC011 gc011)
        {
            return "";
        }

        public virtual string sjuploadASM(string businessId, model.ASM.ASMsj objasmsj, model.ASM.ASMgcsj objasmgcsj, model.ASM.ASMobdgcsj objasmobd,int ics)
        {
            return "成功";
        }

        public virtual string sjuploadSDS(string businessId, model.SDS.SDSsj objasmsj, model.SDS.SDSgcsj objasmgcsj, model.ASM.ASMobdgcsj objasmobd)
        {
            return "成功";
        }

        public virtual string sjuploadZYJS(string businessId, model.ZYJS.ZYJSsj objsj, model.ZYJS.ZYJSgcsj objgcsj, model.LUG.LUGobdgcsj objobdgcsj)
        {
            return "成功";
        }

        public virtual string sjuploadLUG(string businessId, model.LUG.LUGsj objsj, model.LUG.LUGgcsj objgcsj, model.LUG.LUGobdgcsj objobdgcsj)
        {
            return "成功";
        }
        public virtual string zmgcsjlug(string businessId, model.LUG.LUGgcsjZM objgcsj)
        { return "1"; }
        public virtual string zmgcsjzyjs(string businessId, model.ZYJS.ZYJSgcsjZM objgcsj)
        { return "1"; }

        public virtual string zmgcsjasm(string businessId, model.ASM.ASMgcsjZM objgcsj)
        {
            //JsonData value = new LitJson.JsonData();
            //value["InspectionNum"] = objgcsj.businessId;//受理编码  
            //value["StationCode"] = m_mainform.stationCode;//检测站编码  
            //value["SceneCode"] = objgcsj.lineCode;//检测线编码  
            //value["ProcessTime"] = objgcsj.ProcessTime;          // objgcsj.jcgcdysj;//检测过程数据对应时间  
            //value["Second_NO"] = objgcsj.imslsh;//秒数流水号  
            //value["Flow_Speed"] = objgcsj.m_SpeedGCSJ.ToString("0.00");//逐秒的车速  
            //value["Flow_RotateSpeed"] = objgcsj.m_TrunGCSJ;//逐秒发动机转速  
            //value["Flow_Freeweight"] = objgcsj.m_PWGCSJ.ToString("0.00");//逐秒底盘测功机负载  
            //value["Flow_HC"] = objgcsj.m_HCGCSJ.ToString("0");//逐秒HC浓度值(未经修正)  
            //value["Flow_CO"] = objgcsj.m_COGCSJ.ToString("0.00");//逐秒CO浓度值(未经修正)  
            //value["Flow_CO2"] = objgcsj.m_CO2GCSJ.ToString("0.00");//逐秒CO2浓度值(未经修正)  
            //value["Flow_NO"] = objgcsj.m_NOGCSJ.ToString("0"); ;//逐秒NO浓度值(未经修正)  
            //value["Flow_O2"] = objgcsj.m_O2GCSJ.ToString("0.00");//逐秒O2浓度值(未经修正)  
            //value["Flow_HCCorrect"] = objgcsj.m_HCGCSJxz.ToString("0"); ;//逐秒HC浓度值(修正后)  
            //value["Flow_COCorrect"] = objgcsj.m_COGCSJxz.ToString("0.00");//逐秒CO浓度值(修正后)  
            //value["Flow_CO2Correct"] = objgcsj.m_CO2GCSJ.ToString("0.00");//逐秒CO2浓度值(修正后)  
            //value["Flow_NOCorrect"] = objgcsj.m_NOGCSJxz.ToString("0.00");//逐秒NO浓度值(修正后)  
            //value["Flow_O2Correct"] = objgcsj.m_O2GCSJ.ToString("0.00");//逐秒O2浓度值(修正后)  
            //value["Flow_Lambda"] = objgcsj.m_LAMDAGCSJ.ToString("0.00");//逐秒计算的λ值  
            //value["DiluteCorrect"] = objgcsj.m_DFGCSJ.ToString("0.00");//逐秒稀释修正系数DF  
            //value["NOHumidityCorrect"] = objgcsj.m_KHGCSJ.ToString("0.00");//NO湿度修正系数  
            //value["FlowAirPressure"] = objgcsj.sdaqiyi.ToString("0.00");//objgcsj.m_lljqy;//测试工况流量计气压力每秒数据              
            //value["FlowTemperature"] = objgcsj.swendu.ToString("0.00");// objgcsj.m_lljwd;//测试工况流量计温度每秒数据  
            //value["EnvirTemperature"] = objgcsj.swendu.ToString("0.00");//测试工况环境温度每秒数据  
            //value["EnvirAirPressure"] = objgcsj.sdaqiyi.ToString("0.00");//测试工况环境大气压力每秒数据  
            //value["EnvirHumidity"] = objgcsj.sshidu.ToString("0.00");//测试工况环境相对湿度每秒数据  
            //value["NF"] = objgcsj.m_NLGCSJ.ToString("0.00");//扭力  
            //value["NFJZXS"] = objgcsj.m_nlxzxs.ToString("0.00");//扭力校准系数  
            //value["NFXS"] = objgcsj.m_nlxs.ToString("0.00");//扭力系数  
            //value["GKLX"] = objgcsj.GKLX;//工况过程类型  
            //value["Weight_HC"] = objgcsj.m_WeightHC.ToString("0.00");//HC排放质量每秒数据
            //value["Weight_CO"] = objgcsj.m_WeightCO.ToString("0.00");//CO排放质量每秒数据
            //value["Weight_NO"] = objgcsj.m_WeightNO.ToString("0.00");//NO排放质量每秒数据
            //value["ActPower"] = objgcsj.m_ActPower.ToString("0.00");//实测加载功率
            //string svalue = value.ToJson();
            //System.Threading.Thread.Sleep(2000);
            ////m_mainform.baocunriji(svalue);
            //Write("过程数据.txt", DateTime.Now.ToString() + "=" + svalue + "\r\n");
            return "1";
        }
        public virtual string zmgcsjsds(string businessId, model.SDS.SDSgcsjZM objgcsj)
        { return "1"; }

        public virtual string jianceqianzj(string sjylsh, model.尾气自检.检测前自检.ziasm objzj)
        {
            return "成功";
        }

        public virtual string jianceqianzj(string sjylsh, model.尾气自检.检测前自检.zijiansds objzj)
        {
            return "成功";
        }

        public virtual string jianceqianzj(string sjylsh, model.尾气自检.检测前自检.zijianlug objzj)
        {
            return "成功";
        }

        public virtual string jianceqianzj(string sjylsh, model.尾气自检.检测前自检.zijianzyjs objzj)
        {
            return "成功";
        }
        public virtual string uploadjd(string dtCaliDate, string dtCaliValidDate, string szCaliSN)//提交设备校准/检定(计量检定/校准)信息接口
        {

            return "本地上传";
        }
        public virtual string uploadhxjc(model.尾气自检.hxjc objhxjc)//提交设备校准/检定(计量检定/校准)信息接口
        {

            return "本地上传";
        }
        public virtual string uploadzj(model.尾气自检.xljc objxljc)//日常检查泄露检查结束后提交检查结果
        {
            return "本地上传";
        }
        public virtual string uploadzj(model.尾气自检.yliangcheng objxljc)//5.6.4	废气分析仪氧量程检查
        {
            return "本地上传";
        }
        public virtual string uploadzj(model.尾气自检.fqyddjc objxljc)//废气分析仪单点检查
        {
            return "本地上传";
        }
        public virtual string uploadzj(model.尾气自检.btgydjjc objxljc)//
        {
            return "本地上传";
        }
        public virtual string uploadzj(model.尾气自检.btgnoxyqjd objxljc)//
        {
            return "本地上传";
        }
        public virtual string uploadzj(model.尾气自检.dzqxzzijian objxljc)//
        {
            return "本地上传";
        }
        public virtual string uploadzj(model.尾气自检.fjgl objxljc)
        {
            return "本地上传";
        }

        public virtual string uploadzj(联网通信协议.云景model.HJ24 objbdcy)
        {
            return "本地上传";
        }

        public virtual string uploadzj(联网通信协议.云景model.HJ25 objbdcy)
        {
            return "本地上传";
        }
        //------------以下云景联网时小冯加的

        public virtual string baocunclexxyj(string slsh)//车辆信息写入配置文件 车辆信息.ini";
        {
            return "";
        }
        public virtual string sendOBDGCSJ(string sjylsh, string stime, int simslsh)
        {
            return "1";
        }
        public virtual string uploadzj(string tph,model.尾气自检.cyhxsj objxljc)//9.30	上报柴油车底盘测功机滑行检查记录表
        {
            return "本地上传";
        }
        public virtual string uploadzj(model.尾气自检.cyfjgl objxljc)//9.30	上报柴油车底盘测功机滑行检查记录表
        {
            return "本地上传";
        }
        public virtual 报告单.modelbgd getbaogaodan(string slsh)//获取登录车辆上线
        {
            return new 报告单.modelbgd();
        }


        ///中科宇图
        ///
        public virtual string XXTZ(string body)//监控状态
        {
            return "";
        }

        public virtual string OBDYQ()//监控状态
        {
            return "";
        }
        public virtual string OBDDJ()//监控状态
        {
            return "";
        }

        public virtual string  sendOBDJCJG(string yeid, string sjylsh, bool bqz)
        {
            return "1";
        }
        /// <summary>
        /// 获取中科OBD发送流水号
        /// </summary>
        /// <returns></returns>
        public virtual string getobdid( string shphm)//
        {
            return "1";
        }

        public virtual string upzjgcsj(model.尾气自检.zjgcsj objgcsj)
        {
            return "";
        }

        public virtual string uploadzj( model.尾气自检.jsglv objxljc)//9.30	上报柴油车底盘测功机滑行检查记录表
        {
            return "本地上传";
        }
        public virtual string uploadzj(model.尾气自检.JDhuaxing objhuaxing)//上传检定结果
        {
            return "本地上传";
        }

        public virtual string uploadjdsd()//上传检定结果
        {
            return "本地上传";
        }

        public virtual string uploadjdli()//上传检定结果
        {
            return "本地上传";
        }
        public virtual string uploadzhuansu(string sjieguo)
        {
            return "";
        }

        public void Write(string path, string str)
        {
            FileStream fs = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.Write((str).ToString());
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }

        public virtual string GetPIC(string szpdh)
        {
            return "本地上传";
        }
    }
}
